package com.xtyhz.controller;

import com.xtyhz.pojo.dto.ChatRequest;
import com.xtyhz.service.DeepSeekR1;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/deepseek")
public class DeepSeekR1Controller {

    @Autowired
    private DeepSeekR1 deepSeekR1;
    /**
     * 做流式输出到前端
     * @param prompt 前端参数
     * @return 返回大模型数据
     * @throws IOException io异常
     */
    @CrossOrigin
    @GetMapping(value = "/ai/chat")
    public String chat(String prompt, HttpServletResponse res) throws IOException {
        //开始时间
        long startTime = System.currentTimeMillis();

        //调用deepseek官网
        String deepSeekInvoke = deepSeekR1.deepSeek(prompt,res);

        //结束时间
        long endTime = System.currentTimeMillis();
        System.out.println("--------costTime:"+(endTime - startTime)+" 毫秒");

        return deepSeekInvoke;
    }

    @CrossOrigin
    @PostMapping(value = "/ai/chat1",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public String chat1(@RequestBody ChatRequest request, HttpServletResponse res) throws IOException {
        //开始时间
        long startTime = System.currentTimeMillis();

        //调用deepseek官网
        String deepSeekInvoke = deepSeekR1.deepSeek(request.getMessage(),res);

        //结束时间
        long endTime = System.currentTimeMillis();
        System.out.println("--------costTime:"+(endTime - startTime)+" 毫秒");

        return deepSeekInvoke;
    }
}
